﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using System.Web.UI.WebControls;
using Icoders.MarketTracker.Reports.Lib.Domain;
using Icoders.MarketTracker.Reports.Lib.Repository.Transactions;
using Icoders.MarketTracker.Reports.Lib.Util;
using Icoders.MarketTracker.Web.Lib.Services;
using Microsoft.Reporting.WebForms;
using StructureMap;

namespace Icoders.MarketTracker.Web.rpt.sales
{
    public partial class salebysalerep : System.Web.UI.Page
    {
        IDropDownService _dropDownService = ObjectFactory.GetInstance<IDropDownService>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDropDown();
                TextBoxEndDate.Text = DateTime.Now.ToString("dd-MMM-yyyy");
                TextBoxStartDate.Text = DateTime.Now.AddDays(-2).ToString("dd-MMM-yyyy");
            }
        }
        private void LoadDropDown()
        {
            List<SelectListItem> productList = new List<SelectListItem>();
            productList.Add(new SelectListItem { Text = "ALL", Value = "" });
            _dropDownService.GetProduct().ForEach(f => productList.Add(new SelectListItem { Text = f.Text, Value = f.Value }));
            DropDownListProduct.DataSource = productList;
            DropDownListProduct.DataValueField = "Value";
            DropDownListProduct.DataTextField = "Text";
            DropDownListProduct.DataBind();
            List<SelectListItem> countryList = new List<SelectListItem>();
            countryList.Add(new SelectListItem { Text = "ALL", Value = "" });
            _dropDownService.GetCountry().ForEach(f => countryList.Add(new SelectListItem { Text = f.Text, Value = f.Value }));
            DropDownListCountry.DataSource = countryList;
            DropDownListCountry.DataValueField = "Value";
            DropDownListCountry.DataTextField = "Text";
            DropDownListCountry.DataBind();

            ClearCityDropDown();

            ClearAreaDropDown();

            ClearZoneDropDown();

            ClearPOPDropDown();

            ClearFSRDropDown();
        }

        private void ClearFSRDropDown()
        {
            List<SelectListItem> fsrList = new List<SelectListItem>();
            fsrList.Add(new SelectListItem { Text = "ALL", Value = "" });
            DropDownListFSR.DataSource = fsrList;
            DropDownListFSR.DataValueField = "Value";
            DropDownListFSR.DataTextField = "Text";
            DropDownListFSR.DataBind();
        }

        private void ClearPOPDropDown()
        {
            List<SelectListItem> popList = new List<SelectListItem>();
            popList.Add(new SelectListItem { Text = "ALL", Value = "" });
            DropDownListPOP.DataSource = popList;
            DropDownListPOP.DataValueField = "Value";
            DropDownListPOP.DataTextField = "Text";
            DropDownListPOP.DataBind();
        }

        private void ClearZoneDropDown()
        {
            List<SelectListItem> zoneList = new List<SelectListItem>();
            zoneList.Add(new SelectListItem { Text = "ALL", Value = "" });
            DropDownListZone.DataSource = zoneList;
            DropDownListZone.DataValueField = "Value";
            DropDownListZone.DataTextField = "Text";
            DropDownListZone.DataBind();
        }

        private void ClearAreaDropDown()
        {
            List<SelectListItem> AreaList = new List<SelectListItem>();
            AreaList.Add(new SelectListItem { Text = "ALL", Value = "" });
            DropDownListArea.DataSource = AreaList;
            DropDownListArea.DataValueField = "Value";
            DropDownListArea.DataTextField = "Text";
            DropDownListArea.DataBind();
        }

        private void ClearCityDropDown()
        {
            List<SelectListItem> cityList = new List<SelectListItem>();
            cityList.Add(new SelectListItem { Text = "ALL", Value = "" });
            DropDownListCity.DataSource = cityList;
            DropDownListCity.DataValueField = "Value";
            DropDownListCity.DataTextField = "Text";
            DropDownListCity.DataBind();
        }
        protected void ButtonView_Click(object sender, EventArgs e)
        {
            LoadReport();
        }
        private void LoadReport()
        {
            DateTime EndDate = DateTime.Now;
            DateTime StartDate = DateTime.Now.AddDays(-2);
            int product = 0;
            int country = 0;
            int city = 0;
            int area = 0;
            int zone = 0;
            int fsr = 0;
            int pop = 0;
            int.TryParse(DropDownListProduct.Text, out product);
            int.TryParse(DropDownListCountry.Text, out country);
            int.TryParse(DropDownListCity.Text, out city);
            int.TryParse(DropDownListArea.Text, out area);
            int.TryParse(DropDownListZone.Text, out zone);
            int.TryParse(DropDownListFSR.Text, out fsr);
            int.TryParse(DropDownListPOP.Text, out pop);
            DateTime.TryParse(TextBoxEndDate.Text, out EndDate);
            DateTime.TryParse(TextBoxStartDate.Text, out StartDate);
            IReportRepository _reportRepository = ObjectFactory.GetInstance<IReportRepository>();
            ReportViewer1.Reset();
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.ProcessingMode = ProcessingMode.Local;
            ReportViewer1.LocalReport.EnableHyperlinks = true;

            var stream =
                Assembly.GetAssembly(typeof(ReportBase)).GetManifestResourceStream(ReportCollective.SalebySaleRepReport);
            ReportDataSource reportDataSource = new ReportDataSource("SalebySaleRepReportDataset", _reportRepository.GetSaleReport(StartDate, EndDate, product, country, city, area, zone, fsr, pop));
            ReportViewer1.LocalReport.DataSources.Add(reportDataSource);
            ReportViewer1.LocalReport.LoadReportDefinition(stream);

            ReportViewer1.LocalReport.DisplayName = "Sale by sale Rep ";
            ReportViewer1.LocalReport.Refresh();
        }
        protected void DropDownListCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearCityDropDown();
            ClearAreaDropDown();
            ClearZoneDropDown();
            ClearFSRDropDown();
            ClearPOPDropDown();

            int id = 0;
            int.TryParse(DropDownListCountry.Text, out id);
            LoadCity(id);

        }

        private void LoadCity(int countryId)
        {
            List<SelectListItem> cityList = new List<SelectListItem>();
            cityList.Add(new SelectListItem { Text = "ALL", Value = "" });
            _dropDownService.GetCity(countryId).ForEach(f => cityList.Add(new SelectListItem { Text = f.Text, Value = f.Value }));
            DropDownListCity.DataSource = cityList;
            DropDownListCity.DataValueField = "Value";
            DropDownListCity.DataTextField = "Text";
            DropDownListCity.DataBind();
        }

        protected void DropDownListCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearAreaDropDown();
            ClearZoneDropDown();
            ClearFSRDropDown();
            ClearPOPDropDown();
            int id = 0;
            int.TryParse(DropDownListCity.Text, out id);
            LoadArea(id);
        }

        private void LoadArea(int id)
        {
            List<SelectListItem> areaList = new List<SelectListItem>();
            areaList.Add(new SelectListItem { Text = "ALL", Value = "" });
            _dropDownService.GetArea(id).ForEach(f => areaList.Add(new SelectListItem { Text = f.Text, Value = f.Value }));
            DropDownListArea.DataSource = areaList;
            DropDownListArea.DataValueField = "Value";
            DropDownListArea.DataTextField = "Text";
            DropDownListArea.DataBind();
        }

        protected void DropDownListArea_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearZoneDropDown();
            ClearFSRDropDown();
            ClearPOPDropDown();
            int id = 0;
            int.TryParse(DropDownListArea.Text, out id);
            LoadZoneAndFSR(id);
        }

        private void LoadZoneAndFSR(int id)
        {
            List<SelectListItem> zoneList = new List<SelectListItem>();
            zoneList.Add(new SelectListItem { Text = "ALL", Value = "" });
            _dropDownService.GetZone(id).ForEach(f => zoneList.Add(new SelectListItem { Text = f.Text, Value = f.Value }));
            DropDownListZone.DataSource = zoneList;
            DropDownListZone.DataValueField = "Value";
            DropDownListZone.DataTextField = "Text";
            DropDownListZone.DataBind();

            List<SelectListItem> fsrList = new List<SelectListItem>();
            fsrList.Add(new SelectListItem { Text = "ALL", Value = "" });
            _dropDownService.GetFsrByArea(id).ForEach(f => fsrList.Add(new SelectListItem { Text = f.Text, Value = f.Value }));
            DropDownListFSR.DataSource = fsrList;
            DropDownListFSR.DataValueField = "Value";
            DropDownListFSR.DataTextField = "Text";
            DropDownListFSR.DataBind();
        }

        protected void DropDownListZone_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearPOPDropDown();
            int id = 0;
            int.TryParse(DropDownListZone.Text, out id);
            LoadPOP(id);
        }

        private void LoadPOP(int id)
        {
            List<SelectListItem> fsrList = new List<SelectListItem>();
            fsrList.Add(new SelectListItem { Text = "ALL", Value = "" });
            _dropDownService.GetPointofPurchaseByZone(id).ForEach(f => fsrList.Add(new SelectListItem { Text = f.Text, Value = f.Value }));
            DropDownListPOP.DataSource = fsrList;
            DropDownListPOP.DataValueField = "Value";
            DropDownListPOP.DataTextField = "Text";
            DropDownListPOP.DataBind();
        }
    }
}